package view;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;
import javax.swing.border.BevelBorder;

import model.Layer;
import utils.ImageUtils;
import utils.Parameters;

public class LayerListCellRenderer extends JPanel implements ListCellRenderer {

	private JLabel m_previewLabel;
	private JLabel m_titleLabel;

	public JLabel getTitleLabel() {
		return m_titleLabel;
	}

	public void setTitleLabel(JLabel titleLabel) {
		this.m_titleLabel = titleLabel;
	}

	public LayerListCellRenderer() {
		setLayout(new FlowLayout(FlowLayout.LEFT));
		this.m_previewLabel = new JLabel();
		this.m_previewLabel.setPreferredSize(new Dimension(
				Parameters.PREVIEW_SIZE, Parameters.PREVIEW_SIZE));
		m_previewLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
		this.m_titleLabel = new JLabel("untitled");
		this.add(m_previewLabel);

		this.add(m_titleLabel);
		this.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
	}

	@Override
	public Component getListCellRendererComponent(JList list, Object value,
			int index, boolean isSelected, boolean cellHasFocus) {
		Layer layer = (Layer) value;
		if (isSelected && cellHasFocus){
			MainFrame.getInstance().getDrawingPane().setSelectedLayer(layer);
			MainFrame.getInstance().getDrawingPane().setSelectedLayerIndex(index);
		}
		if (cellHasFocus) {
			setBackground(Parameters.HIGHLIGHT_BACKGROUND_CELL_COLOR);
			setForeground(Parameters.HIGHLIGHT_TEXT_CELL_COLOR);
		} else {
			setBackground(Parameters.DEFAULT_BACKGROUND_CELL_COLOR);
			setForeground(Parameters.DEFAULT_TEXT_CELL_COLOR);
		}

		m_previewLabel.setIcon(new ImageIcon(ImageUtils.resizeInferiorTo(
				ImageUtils.toImage(layer.getImage()), 50, 50)));
		m_titleLabel.setText("<" + layer.getTitle() + ">");

		return this;
	}

}
